﻿Array.prototype.contains = function(v) {
    for(var i = 0; i < this.length; i++) {
        if(this[i] === v) return true;
    }
    return false;
};

Array.prototype.unique = function() {
    var arr = [];
    for(var i = 0; i < this.length; i++) {
        if(!arr.contains(this[i])) {
            arr.push(this[i]);
        }
    }
    return arr; 
}

var add_flag = false;
var edit_flag = false;

function ShowAdd() {
    $('#addModal').modal('show');
    InitPaging();

    $("select[id^='ddlAddBobotPillihanJawaban']").change(function () {
        var selecteditems = [];
        var value = $(this).val();
        selecteditems.push(value);

        $("select[id^='ddlAddBobotPillihanJawaban']").not(this).each(function() {
            selecteditems.push($(this).val());
        });

        var count = selecteditems.length;
        selecteditems = selecteditems.unique();

        add_flag = (count == selecteditems.length);
        
    });

    $('#txtAddPertanyaan').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddPilihanJawaban1').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddPilihanJawaban2').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddPilihanJawaban3').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddPilihanJawaban4').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddBobotPillihanJawaban1').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtAddBobotPillihanJawaban2').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });
    $('#txtAddBobotPillihanJawaban3').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });
    $('#txtAddBobotPillihanJawaban4').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });


    $('#btAddPertanyaan').click(function () {
        if (($('#txtAddPertanyaan').val().length == 0) || ($('#txtAddPilihanJawaban1').val().length == 0) || ($('#txtAddPilihanJawaban2').val().length == 0) || ($('#txtAddPilihanJawaban3').val().length == 0) || ($('#txtAddPilihanJawaban4').val().length == 0) || (!add_flag)) {
            return false;
        }
    });
}

function ShowEdit() {
    $('#editModal').modal('show');
    InitPaging();

    $("select[id^='ddlEditBobotPillihanJawaban']").change(function () {
        var selecteditems = [];
        var value = $(this).val();
        selecteditems.push(value);

        $("select[id^='ddlEditBobotPillihanJawaban']").not(this).each(function () {
            selecteditems.push($(this).val());
        });

        var count = selecteditems.length;
        selecteditems = selecteditems.unique();

        edit_flag = (count == selecteditems.length);
    });

    $('#txtEditPertanyaan').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtEditPilihanJawaban1').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtEditPilihanJawaban2').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtEditPilihanJawaban3').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtEditPilihanJawaban4').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#txtEditBobotPillihanJawaban1').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });
    $('#txtEditBobotPillihanJawaban2').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });
    $('#txtEditBobotPillihanJawaban3').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });
    $('#txtEditBobotPillihanJawaban4').keydown(function (e) {
        if (e.keyCode === 13 && !e.shiftKey) {
            e.preventDefault();
        }
    });

    $('#btEditPertanyaan').click(function () {
        if (($('#txtEditRekomendasi').val().length == 0) || ($('#txtEditPilihanJawaban1').val().length == 0) || ($('#txtEditPilihanJawaban2').val().length == 0) || ($('#txtEditPilihanJawaban3').val().length == 0) || ($('#txtEditPilihanJawaban4').val().length == 0) || (!edit_flag)) {
            return false;
        }
    });
}

function ShowDelete() {
    $('#deleteModal').modal('show');
    InitPaging();
}

function ShowDetail() {
    $('#detailModal').modal('show');
    InitPaging();
}

function SuccessAdd() {
    $('#addModal').modal('hide');
    InitPaging();
}

function SuccessEdit() {
    $('#editModal').modal('hide');
    InitPaging();
}

function SuccessDelete() {
    $('#deleteModal').modal('hide');
    InitPaging();
}